import request from "@/axios.js";
import avueDataAdapter from "@/utils/avueDataAdapter";
import activitiFormUtils from "./activitiFormUtils";
import startCrudTools from "@/mixins/startCrudTools";
import startSearchModel from "@/config/startSearchModel";

const optionSearchCache = {};
const defaultLoadParams = {
    columnKey: "column",
}

function loadSearchCache(columns) {
    let columnsMap = {};
    if (!columns) return columnsMap;
    for (let i = 0; i < columns.length; i++) {
        let column = columns[i];
        if (!column.prop) continue;
        if (!column.search) continue
        columnsMap[column.prop] = column.searchModel ? column.searchModel : startSearchModel.EQ;
    }
    return columnsMap;
}


/**
 * 获取搜索列信息
 * @param option
 * @param cacheKey
 * @returns {{}}
 */
function getSearchColumnMap(option, cacheKey) {
    let columnsMap = {};
    if (option == null) return columnsMap;
    let optionConfig = null;
    let key = cacheKey;
    if (option instanceof Function) {
        optionConfig = option();
    } else if (option instanceof Object) {
        optionConfig = option;
    } else if (option instanceof Array) {
        optionConfig = {column: option};
    } else {
        return columnsMap;
    }
    let columns = optionConfig.column;
    if (!columns) return columnsMap;

    if (!key)
        key = optionConfig.cacheKey;
    if (!key)
        return loadSearchCache(columns)
    if (optionSearchCache[key])
        return optionSearchCache[key];
    columnsMap = loadSearchCache(columns);
    optionSearchCache[key] = columnsMap;
    return optionSearchCache[key];
}

function tableOnLoad(onLoadParams, callback) {
    if (onLoadParams.value) {
        loadValue(onLoadParams.value, onLoadParams.valueDefinition, callback)
        return
    }
    loadPage(onLoadParams, callback)
}

const optionUtils = {
    getSearchColumnMap,
    tableOnLoad
}

/**
 * 加载分页信息
 * @param loadParams
 * @param callback
 */
function loadPage(loadParams, callback) {
    loadParams = Object.assign(defaultLoadParams, loadParams)
    let pageParams = {}
    let formSearchParams = loadParams.data;
    let customSearchParams = loadParams.searchParams || {};
    const page = loadParams.page
    pageParams.current = page.currentPage
    pageParams.size = page.pageSize;
    const tableOption = loadParams.option instanceof Function ? loadParams.option() : loadParams.option
    let columnsMap = optionUtils.getSearchColumnMap(loadParams.option);
    let searchParams = startCrudTools.getSearchParams(formSearchParams, columnsMap)
    const requestData = Object.assign(pageParams, searchParams, customSearchParams)
    if (loadParams.pageDefinition instanceof Function) {
        loadParams.pageDefinition(requestData).then(callBackRequest)
        return
    }

    request({
        url: loadParams.pageDefinition,
        method: 'get',
        data: requestData
    }).then(callBackRequest, callback)

    function callBackRequest(res) {
        let resultData = res.data.data;
        const records = resultData.records;
        avueDataAdapter.transformDataList(tableOption.column, records)
        callback({
            data: records,
            total: resultData.total
        })
    }


}


/**
 * 加载固定值信息
 * @param value
 * @param valueDefinition
 * @param callback
 */
function loadValue(value, valueDefinition, callback) {
    if (valueDefinition instanceof Function) {
        valueDefinition(value).then(res => callback(res.data.data))
        return
    }
    let data = {};
    data.value = value
    request({
        url: valueDefinition,
        method: 'get',
        data: data
    }).then(res => callback(res.data.data))

}


optionUtils.handlerFormConfigVariable = (formOption) => {
    return activitiFormUtils.handlerFormConfigVariable(formOption)
}

export default optionUtils
